package org.xq.shop.maxaishop.service.impl;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.xq.shop.maxaishop.dao.XqAdminDao;
import org.xq.shop.maxaishop.entity.XqAdmin;
import org.xq.shop.maxaishop.entity.XqCustomer;
import org.xq.shop.maxaishop.service.XqAdminService;

import javax.annotation.Resource;
import java.util.List;

/**
 * (XqAdmin)表服务实现类
 *
 * @author makejava
 * @since 2021-06-11 18:17:22
 */
@Service("xqAdminService")
public class XqAdminServiceImpl implements XqAdminService, UserDetailsService {
    @Resource
    private XqAdminDao xqAdminDao;
    @Resource
    private BCryptPasswordEncoder passwordEncoder;
    /**
     * 通过ID查询单条数据
     *
     * @param aid 主键
     * @return 实例对象
     */
    @Override
    public XqAdmin queryById(Integer aid) {
        return this.xqAdminDao.queryById(aid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<XqAdmin> queryAllByLimit(int offset, int limit) {
        return this.xqAdminDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param xqAdmin 实例对象
     * @return 实例对象
     */
    @Override
    public XqAdmin insert(@RequestBody XqAdmin xqAdmin) {
        String encode = passwordEncoder.encode(xqAdmin.getPassword());
        xqAdmin.setPassword(encode);
        this.xqAdminDao.insert(xqAdmin);
        return xqAdmin;
    }

    @Override
    public XqAdmin queryByNameAndPwd(String name, String pwd) {
        return this.xqAdminDao.queryByNameAndPwd(name,pwd);
    }

    /**
     * 修改数据
     *
     * @param xqAdmin 实例对象
     * @return 实例对象
     */
    @Override
    public int update(@RequestBody XqAdmin xqAdmin) {
        return this.xqAdminDao.update(xqAdmin);
    }

    @Override
    public int updateStatus(int aid) {
        return this.xqAdminDao.updateStatus(aid);
    }

    @Override
    public int countt(String aname) {
        return this.xqAdminDao.countt(aname);
    }

    @Override
    public int updatePwd(String newpwd, String uname) {
        return this.xqAdminDao.updatePwd(newpwd,uname);
    }

    @Override
    public XqAdmin selectByAname(String username) {
        return this.xqAdminDao.selectByAname(username);
    }

    /**
     * 通过主键删除数据
     *
     * @param aid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer aid) {
        return this.xqAdminDao.deleteById(aid) > 0;
    }

    @Override
    public int registered(String aname, String apwd, String atel, String email) {
        String pwd = passwordEncoder.encode(apwd);
        return this.xqAdminDao.registered(aname,pwd,atel,email);
    }

    @Override
    public List<XqCustomer> selectAll(Integer start, Integer end) {
        return this.xqAdminDao.selectAll(start,end);
    }

    @Override
    public List<XqCustomer> selectByUname(String aname, Integer start, Integer end) {
        return this.xqAdminDao.selectByUname(aname,start,end);
    }

    @Override
    public int selectCount() {
        return this.xqAdminDao.selectCount();
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        XqAdmin xqAdmin = xqAdminDao.queryByName(username);
        System.out.println("=====" + passwordEncoder.encode("123"));
        if (xqAdmin == null) {
            throw new UsernameNotFoundException("找不到该用户！");
        }
        return xqAdmin;
    }
}
